Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
The vinyl-fs npm package is a module that allows you to handle file operations in a virtual file format known as Vinyl. It is commonly used in the Gulp build system to read and write files, watch file changes, and manage file streams. Vinyl-fs abstracts file details and provides a consistent API to manipulate files regardless of the source (local file system, remote, etc.).
Reading Files
This feature allows you to read files from the filesystem using glob patterns. The example code demonstrates how to read all JavaScript files in the 'src' directory and log their paths.
const { src } = require('vinyl-fs');
src('src/*.js')
.on('data', function(file) {
console.log(file.path);
});
Writing Files
This feature enables writing files to a specified directory. The code sample shows how to copy all JavaScript files from the 'src' directory to the 'output' directory.
const { src, dest } = require('vinyl-fs');
src('src/*.js')
.pipe(dest('output/'));
Watching File Changes
This feature is useful for setting up a watch on files to perform actions when changes are detected. The example sets up a watcher on all JavaScript files in the 'src' directory and logs a message whenever a file changes.
const { watch } = require('vinyl-fs');
watch('src/**/*.js', function(cb) {
console.log('File changed.');
cb();
});
graceful-fs is a drop-in replacement for the fs module with improvements for handling file system operations more gracefully. It does not provide the virtual file abstraction that vinyl-fs offers but enhances file system reliability, especially under high load.
through2 is a thin wrapper around Node.js streams.Transform (Streams2/3) to avoid explicit subclassing noise. It's similar to vinyl-fs in that it helps with stream manipulation, but it does not deal directly with file system operations or virtual file objects.
fs-extra adds file system methods that aren't included in the native fs module and adds promise support to fs methods. It is similar to vinyl-fs in providing more utilities for file operations, but it does not use the Vinyl abstraction or integrate directly with streaming pipelines.
Vinyl adapter for the file system.
Vinyl is a very simple metadata object that describes a file. When you think of a file, two attributes come to mind: path
and contents
. These are the main attributes on a Vinyl object. A file does not necessarily represent something on your computer’s file system. You have files on S3, FTP, Dropbox, Box, CloudThingly.io and other services. Vinyl can be used to describe files from all of these sources.
While Vinyl provides a clean way to describe a file, we now need a way to access these files. Each file source needs what we call a "Vinyl adapter". A Vinyl adapter simply exposes a src(globs)
and a dest(folder)
method. Each return a stream. The src
stream produces Vinyl objects, and the dest
stream consumes Vinyl objects. Vinyl adapters can expose extra methods that might be specific to their input/output medium, such as the symlink
method vinyl-fs
provides.
var map = require('map-stream');
var vfs = require('vinyl-fs');
var log = function(file, cb) {
console.log(file.path);
cb(null, file);
};
vfs.src(['./js/**/*.js', '!./js/vendor/*.js'])
.pipe(map(log))
.pipe(vfs.dest('./output'));
src(globs[, options])
Takes a glob string or an array of glob strings as the first argument and an options object as the second.
Returns a stream of vinyl File
objects.
Note: UTF-8 BOM will be removed from all UTF-8 files read with .src
unless disabled in the options.
Globs are executed in order, so negations should follow positive globs.
For example:
fs.src(['!b*', '*'])
would not exclude any files, but the following would exclude all files starting with "b":
fs.src(['*', '!b*'])
File
object as its only argument and must return a value of the expected type for that option.options.buffer
Whether or not you want to buffer the file contents into memory. Setting to false
will make file.contents
a paused Stream.
Type: Boolean
Default: true
options.read
Whether or not you want the file to be read at all. Useful for stuff like removing files. Setting to false
will make file.contents = null
and will disable writing the file to disk via .dest()
.
Type: Boolean
Default: true
options.since
Only streams files that have been modified since the time specified.
Type: Date
or Number
Default: undefined
options.removeBOM
Causes the BOM to be removed on UTF-8 encoded files. Set to false
if you need the BOM for some reason.
Type: Boolean
Default: true
options.sourcemaps
Enables sourcemap support on files passed through the stream. Will load inline sourcemaps and resolve sourcemap links from files.
Type: Boolean
Default: false
options.resolveSymlinks
Whether or not to recursively resolve symlinks to their targets. Set to false
to preserve them as symlinks and make file.symlink
equal the original symlink's target path.
Type: Boolean
Default: true
options.dot
Whether or not you want globs to match on dot files (e.g. .gitignore
).
Note: This option is not resolved from a function because it is passed verbatim to node-glob.
Type: Boolean
Default: false
Any glob-related options are documented in glob-stream and node-glob and are forwarded verbatim.
dest(folder[, options])
Takes a folder path string or a function as the first argument and an options object as the second. If given a function, it will be called with each vinyl File
object and must return a folder path.
Returns a stream that accepts vinyl File
objects, writes them to disk at the folder/cwd specified, and passes them downstream so you can keep piping these around.
Once the file is written to disk, an attempt is made to determine if the stat.mode
, stat.mtime
and stat.atime
of the vinyl File
object differ from the file on the filesystem.
If they differ and the running process owns the file, the corresponding filesystem metadata is updated.
If they don't differ or the process doesn't own the file, the attempt is skipped silently.
This functionality is disabled on Windows operating systems or any other OS that doesn't support process.getuid
or process.geteuid
in node. This is due to Windows having very unexpected results through usage of fs.fchmod
and fs.futimes
.
Note: The fs.futimes()
method internally converts stat.mtime
and stat.atime
timestamps to seconds; this division by 1000
may cause some loss of precision in 32-bit Node.js.
If the file has a symlink
attribute specifying a target path, then a symlink will be created.
Note: The file will be modified after being written to this stream.
cwd
, base
, and path
will be overwritten to match the folder.stat
will be updated to match the file on the filesystem.contents
will have it's position reset to the beginning if it is a stream.File
object as its only argument and must return a value of the expected type for that option.options.cwd
The working directory the folder is relative to.
Type: String
Default: process.cwd()
options.mode
The mode the files should be created with. This option is only resolved if the vinyl File
is not symbolic.
Type: Number
Default: The mode
of the input file (file.stat.mode
) if any, or the process mode if the input file has no mode
property.
options.dirMode
The mode directories should be created with.
Type: Number
Default: The process mode
.
options.overwrite
Whether or not existing files with the same path should be overwritten.
Type: Boolean
Default: true
(always overwrite existing files)
options.append
Whether or not new data should be appended after existing file contents (if any).
Type: Boolean
Default: false
(always replace existing contents, if any)
options.sourcemaps
Enables sourcemap support on files passed through the stream. Will write inline soucemaps if specified as true
.
Specifying a String
path will write external sourcemaps at the given path.
Examples:
// Write as inline comments
vfs.dest('./', { sourcemaps: true });
// Write as files in the same folder
vfs.dest('./', { sourcemaps: '.' });
Type: Boolean
or String
Default: undefined
(do not write sourcemaps)
options.relativeSymlinks
When creating a symlink, whether or not the created symlink should be relative. If false
, the symlink will be absolute.
Note: This option will be ignored if a junction
is being created, as they must be absolute.
Type: Boolean
Default: false
options.useJunctions
When creating a symlink, whether or not a directory symlink should be created as a junction
.
This option is only relevant on Windows and ignored elsewhere. Please refer to the Symbolic Links on Windows section below.
Type: Boolean
Default: true
symlink(folder[, options])
Takes a folder path string or a function as the first argument and an options object as the second. If given a function, it will be called with each vinyl File
object and must return a folder path.
Returns a stream that accepts vinyl File
objects, creates a symbolic link (i.e. symlink) at the folder/cwd specified, and passes them downstream so you can keep piping these around.
Note: The file will be modified after being written to this stream.
cwd
, base
, and path
will be overwritten to match the folder.stat
will be updated to match the symlink on the filesystem.contents
will be set to null
.symlink
will be added or replaced to be the original path.Note: On Windows, directory links are created using Junctions by default. Use the useJunctions
option to disable this behavior.
File
object as its only argument and must return a value of the expected type for that option.options.cwd
The working directory the folder is relative to.
Type: String
Default: process.cwd()
options.dirMode
The mode directories should be created with.
Type: Number
Default: The process mode.
options.overwrite
Whether or not existing files with the same path should be overwritten.
Type: Boolean
Default: true
(always overwrite existing files)
options.relativeSymlinks
Whether or not the created symlinks should be relative. If false
, the symlink will be absolute.
Note: This option will be ignored if a junction
is being created, as they must be absolute.
Type: Boolean
Default: false
options.useJunctions
When creating a symlink, whether or not a directory symlink should be created as a junction
.
This option is only relevant on Windows and ignored elsewhere. Please refer to the Symbolic Links on Windows section below.
Type: Boolean
Default: true
When creating symbolic links on Windows, we pass a type
argument to Node's
fs
module which specifies the kind of target we link to (one of 'file'
,
'dir'
or 'junction'
). Specifically, this will be 'file'
when the target
is a regular file, 'junction'
if the target is a directory, or 'dir'
if
the target is a directory and the user overrides the useJunctions
option
default.
However, if the user tries to make a "dangling" link (pointing to a non-existent
target) we won't be able to determine automatically which type we should use.
In these cases, vinyl-fs
will behave slightly differently depending on
whether the dangling link is being created via symlink()
or via dest()
.
For dangling links created via symlink()
, the incoming vinyl represents the
target and so we will look to its stats to guess the desired type. In
particular, if isDirectory()
returns false then we'll create a 'file'
type
link, otherwise we will create a 'junction'
or a 'dir'
type link depending
on the value of the useJunctions
option.
For dangling links created via dest()
, the incoming vinyl represents the link -
typically read off disk via src()
with the resolveSymlinks
option set to
false. In this case, we won't be able to make any reasonable guess as to the
type of link and we default to using 'file'
, which may cause unexpected behavior
if you are creating a "dangling" link to a directory. It is advised to avoid this
scenario.
FAQs
Vinyl adapter for the file system.
The npm package vinyl-fs receives a total of 2,078,734 weekly downloads. As such, vinyl-fs popularity was classified as popular.
We found that vinyl-fs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.